package org.gpswalker.sys.server.misc;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

//import ru.tr0.dic.login.UserInfo;

public class ServletInOut {
	private HttpServletRequest req;
	private HttpServletResponse res;
	private PrintWriter out;
	
	public String getStringFromReq (String param) {
		return Misc.getStringFromReq(req, param);
	}
	public Object getParam(String param){
		return req.getSession().getAttribute(param);
	}
	public String[] getParamValues(String param){
		return  req.getParameterValues(param);
	}

	public int getIntFromReq (String param){
		return Misc.getIntFromReq(req, param);
	}
	
	public HttpServletRequest getRequest() {
		return req;
	}

	public HttpServletResponse getResponse() {
		return res;
	}

	public PrintWriter getWriter() {
		return out;
	}
	
	public void print(String s) {
		out.print(s);
	}
	public void println(String s) {
		out.println("<p>" + s +"</p>");
	}

//	public UserInfo getUserInfo() {
//		return (UserInfo) req.getSession().getAttribute("userInfo");
//	}

	private void makeWriter() {
//		res.setContentType("text/html; charset=utf8");
		try {
			out = res.getWriter();
		} catch (IOException e) {
//			Logger.getInstance().log(Misc.getStackTrace(e));
		}
	}

	public ServletInOut(HttpServletRequest req_, HttpServletResponse res_) {
		req = req_;
		res = res_;
//		makeWriter();
	}

	
	public Key getKeyFromReq(String keyParam) {
	      String sectionId = getStringFromReq(keyParam).intern();
	        if (sectionId == "") {
	            return null;
	        }
	        return KeyFactory.stringToKey(sectionId);
	}
	
	
	public String getSessionId() {
	    return getRequest().getSession().getId();
	}
	
}
